<?php

namespace app\models;

use think\Model;

class Order extends Model
{
    protected $pk = 'id';
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'created';
    protected $updateTime = 'updated';

    public function getStatusTextAttr($value, $data)
    {
        $text = '未知';
        switch ($data['status']) {
            case '1':
                //用户交接单
                if ($data['type'] == 1) {
                    if ($data['mconfirm'] == 1 && $data['afconfirm'] == 2) {
                        $text = '待主管再次确认';
                    } elseif ($data['mconfirm'] == 1) {
                        $text = '待主管确认订单';
                    } elseif ($data['afconfirm'] == 1) {
                        $text = '待售后经理确认订单';
                    } elseif ($data['wconfirm'] == 1 && $data['fconfirm'] == 1) {
                        $text = '待仓库、财务确认订单';
                    } elseif ($data['wconfirm'] == 1) {
                        $text = '待仓库确认订单';
                    } elseif ($data['fconfirm'] == 1) {
                        $text = '待财务确认订单';
                    }
                }
                //用户首配单和补货单
                if ($data['type'] == 2 || $data['type'] == 3) {
                    if ($data['mconfirm'] == 1) {
                        $text = '待主管确认订单';
                    } elseif ($data['wconfirm'] == 1 && $data['fconfirm'] == 1) {
                        $text = '待仓库、财务确认订单';
                    } elseif ($data['wconfirm'] == 1) {
                        $text = '待仓库确认订单';
                    } elseif ($data['fconfirm'] == 1) {
                        $text = '待财务确认订单';
                    }
                }
                if ($data['type'] == 4 || $data['type'] == 5) {
                    if ($data['mconfirm'] == 1) {
                        $text = '待主管确认订单';
                    } elseif ($data['wconfirm'] == 1) {
                        $text = '待仓库确认订单';
                    }
                }
                if ($data['type'] == 6) {
                    if ($data['swconfirm'] == 1) {
                        $text = '待超管确认订单';
                    } elseif ($data['wconfirm'] == 1) {
                        $text = '待仓库确认订单';
                    }
                }
                break;
            case '2':
                $text = '已完成';
                break;
            case '3':
                $text = '已取消';
                break;
            default:
                # code...
                break;
        }
        return $text;
    }

    //订单操作状态获取 $utype用户等级
    public function getconfirms($row, $utype)
    {
        $row['confirm'] = '';
        if ($row['status'] != 1) {
            return $row['confirm'];
        }
        //对接单 管理和售后 财务确认
        if ($row['type'] == 1) {
            //主管确认
            if ($utype == '9' && $row['mconfirm'] == 1) {
                $row['confirm'] = 'mconfirm'; //主管
            } else if ($utype == '2' && $row['mconfirm'] == 2 && $row['afconfirm'] == 1) {
                $row['confirm'] = 'afconfirm'; //售后
            } else if ($utype == '4' && $row['afconfirm'] == 2 && $row['wconfirm'] == 1) {
                $row['confirm'] = 'wconfirm'; //仓库确认
            } else if ($utype == '3' && $row['afconfirm'] == 2 && $row['fconfirm'] == 1) {
                $row['confirm'] = 'fconfirm'; //财务确认
            }
        }
        //首配单/补货单 管理和仓库 财务确认
        if ($row['type'] == 2 || $row['type'] == 3) {
            if ($utype == '9' && $row['mconfirm'] == 1) {
                $row['confirm'] = 'mconfirm';
            } else if ($utype == '4' && $row['mconfirm'] == 2 && $row['wconfirm'] == 1) {
                $row['confirm'] = 'wconfirm';
            } else if ($utype == '3' && $row['mconfirm'] == 2 && $row['fconfirm'] == 1) {
                $row['confirm'] = 'fconfirm';
            }
        }
        //物料采购单 财务确认 仓库确认 仓库财务确认  财务确认
        if ($row['type'] == 4 || $row['type'] == 5) {
            if ($utype == '9' && $row['mconfirm'] == 1) {
                $row['confirm'] = 'mconfirm';
            } else if ($utype == '4' && $row['mconfirm'] == 2 && $row['wconfirm'] == 1) {
                $row['confirm'] = 'wconfirm';
            }
        }
        //收银机采购单 财务确认
        if ($row['type'] == 6) {
            if ($utype == '10' && $row['swconfirm'] == 1) {
                $row['confirm'] = 'swconfirm';
            } else if ($utype == '4' && $row['swconfirm'] == 2 && $row['wconfirm'] == 1) {
                $row['confirm'] = 'wconfirm';
            }
        }
        return $row['confirm'];
    }

    //订单完结判断和结算
    public function finishs($id)
    {
        $order = $this->where(['id' => $id, 'status' => 1, 'del' => 1])->find();
        if (empty($order)) {
            throw new \Exception("订单不存在或完结，请刷新重试", 100);
        }
        //完结
        if ($order['mconfirm'] != 1 && $order['afconfirm'] != 1 && $order['fconfirm'] != 1 && $order['wconfirm'] != 1 && $order['swconfirm'] != 1) {
            //库存计算
            $goods = (new Ordergoods())->where(['orderid' => $order['id'], 'del' => 1])->select();
            //状态变更
            $this->where('id', $order['id'])->update(['status' => 2]);
            //写入或扣除库存
            foreach ($goods as $good) {
                if ($order['type'] == 4 || $order['type'] == 5 || $order['type'] == 6) {
                    $this->GoodSkuEdit($good['goodsid'], $good['number'], $order['type'], $order['id'], '采购订单');
                } else {
                    $this->GoodSkuEdit($good['goodsid'], -$good['number'], $order['type'], $order['id'], '其他订单');
                }
            }
        }
    }

    //库存修改明细
    public static function GoodSkuEdit($goodid, $sku, $type, $orderid, $info)
    {
        if ($sku != 0) {
            $goods = (new Goods())->where('id', $goodid)->find();
            if (empty($goods)) {
                throw new \Exception("商品库存修改失败", 100);
            }
            $res =  (new Goods())->where('id', $goods['id'])->setInc('sku', $sku);

            $res2 = GoodsLog::insert([
                'orderid' => $orderid,
                'time' => time(),
                'goodsid' => $goodid,
                'befores' => $goods['sku'],
                'money' => $sku,
                'afters' => ($goods['sku'] + $sku),
                'type' => $type,
                'info' => $info,
            ]);
            if (!$res || !$res2) {
                throw new \Exception("库存修改失败，请重试", 100);
            }
        }
    }
}
